package com.yf.zzmusic.controller;


import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.music.MusicByIdsDTO;
import com.yf.zzmusic.pojo.dto.music.MusicPageDTO;
import com.yf.zzmusic.pojo.dto.music.MusicUpdateDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import com.yf.zzmusic.service.IMusicService;
import com.yf.zzmusic.pojo.entity.Music;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 音乐表，如果对应的音乐被删除，其系统中的mp3源文件也应该被删除 前端控制器
 * </p>
 *
 * @author
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/music")
public class MusicController {
    @Resource
    private IMusicService musicService;

    @PostMapping("/insert")
    public Result save(@Validated @RequestBody Music music) {
        boolean result=musicService.insertMusic(music);
        if (result){
            return Result.success("新增成功",null);
        }else{
            return Result.error("新增失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result= musicService.removeById(id);
        if (result){
            return Result.success("删除成功",null);
        }else{
            return Result.error(Constants.NO_CONTENT,"删除失败");
        }
    }

    @PostMapping("/update")
    public Result update(@Validated @RequestBody MusicUpdateDTO musicUpdateDTO){
        boolean result=musicService.updateMusic(musicUpdateDTO);
        if (result){
            return Result.success("修改成功",null);
        }else{
            return Result.error("修改失败");
        }
    }



    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Music music=musicService.getById(id);
        if (music!=null){
            return Result.success("查询成功",music);
        }else{
            return Result.error(Constants.NO_CONTENT,"查询失败");
        }
    }

    @GetMapping("/page")
    public Result findPage(@Validated MusicPageDTO musicPageDTO) {
        PageInfo<Music> pageInfo=musicService.musicByPage(musicPageDTO);
        if (pageInfo.getTotal()>0) {
            return Result.success("查询成功",pageInfo);
        }else{
            return Result.error(Constants.NO_CONTENT,"列表为空");
        }
    }

    @PostMapping("/ids")
    public Result selectMusicByIds(@Validated @RequestBody MusicByIdsDTO music){
        List<Music> musicList=musicService.musicByIds(music.getIds());
        if (musicList.size()>0){
            return Result.success("查询成功",musicList);
        }else{
            return Result.error("查询失败");
        }
    }
}

